<%-- 
    Document   : DbAdmin.jsp
    Created on : 19 Aug, 2011, 4:48:22 PM
    Author     : Vivekananda Moosani
--%>

<%@page contentType="text/html" pageEncoding="windows-1252"%>
<%
String datasourceName = (String)session.getAttribute("datasourceName");
if(datasourceName == null) {
    response.sendRedirect("index.jsp");
}

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
        <title>DbAdminstrationScreen</title>
        <link rel="SHORTCUT ICON" href="favico.gif"/>
        
<!-- 
    Dependency configurator URL: http://developer.yahoo.com/yui/articles/hosting/?base&button&connection&datatable&dragdrop&fonts&json&MIN
-->
<!-- Combo-handled YUI CSS files: -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.9.0/build/fonts/fonts-min.css&2.9.0/build/base/base-min.css&2.9.0/build/button/assets/skins/sam/button.css&2.9.0/build/datatable/assets/skins/sam/datatable.css">
<!-- Combo-handled YUI JS files: -->
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.9.0/build/yahoo-dom-event/yahoo-dom-event.js&2.9.0/build/element/element-min.js&2.9.0/build/button/button-min.js&2.9.0/build/connection/connection-min.js&2.9.0/build/dragdrop/dragdrop-min.js&2.9.0/build/datasource/datasource-min.js&2.9.0/build/datatable/datatable-min.js&2.9.0/build/json/json-min.js"></script>


        <script type="text/javascript">
            var cObj;
            function showResults() {
                //display query being executed ..
                document.getElementById("queryExecuted").value = document.getElementById("query").value;
                
                var callback = {
                    cache:false,
                    success: function(o) {
                        try {
                            //figure out cols and resultset
                            var result = YAHOO.lang.JSON.parse(o.responseText);
                            //display results or summary
                            var lastSet = result[result.length-1];
                            if(lastSet.columnDetails) {
                                showResultsTable(lastSet.columnDetails, lastSet.data);
                            } else {
                                document.getElementById("queryResultsTable").innerHTML = "Number of rows updated / modified / deleted: "
                                            + lastSet.updateCount;
                            }
                        } catch(ex) {
                            document.getElementById("queryResultsTable").innerHTML =  "Exception in success handler .. ";
                            alert(o.responseText);
                        }
                    }, 
                    failure: function(o) {
                        document.getElementById("queryResultsTable").innerHTML =  "Something went wrong .. ";
                        alert(o.responseText);
                    }
                }

                document.getElementById("queryResultsTable").innerHTML =  "Executing .. ";
                //make ajax call for fetching results handle success / failure ..
                if(cObj) {
                    YAHOO.util.Connect.abort(cObj, callback);
                }
                YAHOO.util.Connect.setForm(document.getElementById("queryForm"));
                cObj = YAHOO.util.Connect.asyncRequest('POST', 'ExecuteQuery.jsp', callback);
            }

            function showResultsTable(columnDetails, data) {

                var columnDefs = [];
                var resultsDs = new YAHOO.util.DataSource(data);
                resultsDs.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
                resultsDs.responseSchema = {fields:[]};
                for(var i=0; i<columnDetails.length; i++) {
                    columnDefs[i] = {key:columnDetails[i], formatter:"text", sortable:true};
                    resultsDs.responseSchema.fields[i] = {key:columnDetails[i]};
                }
                
                var resultsDt = new YAHOO.widget.DataTable("queryResultsTable", columnDefs, resultsDs,
                                                                    {});
            }

            function cleanDatabase() {
                var callback = {
                    cache:false,
                    success: function(o) {
                        try {
                            //figure out cols and resultset
                            var result = YAHOO.lang.JSON.parse(o.responseText);
                            //display results or summary
                            if(result.allIsWell == true) {
                               document.getElementById("cleanDbProgress").innerHTML = "Database objects deleted";
                            } else {
                               document.getElementById("cleanDbProgress").innerHTML = "Database objects deletion failed";
                               alert("Some thing went wrong " + o.responseText);
                            }
                        } catch(ex) {
                               document.getElementById("cleanDbProgress").innerHTML = "Database objects deletion failed";
                               alert("Some thing went wrong " + o.responseText);
                        }
                    },
                    failure: function(o) {
                        document.getElementById("cleanDbProgress").innerHTML = "Database objects deletion failed";
                        alert(o.responseText);
                    }
                }

                document.getElementById("cleanDbProgress").innerHTML =  "Executing .. ";
                //make ajax call for fetching results handle success / failure ..
                if(cObj) {
                    YAHOO.util.Connect.abort(cObj, callback);
                }
                //YAHOO.util.Connect.setForm(null);
                cObj = YAHOO.util.Connect.asyncRequest('POST', 'DeleteAllObjects.jsp', callback);
            }
        </script>
    </head>
    <body class="yui-skin-sam">
        <h1> Working on datasource: <%=datasourceName%></h1>
        
        <!-- a href="javascript:cleanDatabase()"> Delete all tables in the database</a>&nbsp;&nbsp; <span id="cleanDbProgress"></span><br/>
        <br/-->
        <div> Enter one or more queries below. Note that only results of the last query are shown:
        <form method="POST" id="queryForm">
            <textarea name="query" id="query" style="width:600px;height:400px;"></textarea>
            <br/><br/>
            <div style="width:600px;text-align:right"><input type="button" onclick="showResults()" value="Execute" /></div>
        </form>
        </div>
        <br/>
        <div id="resultsDiv">
            <div id="queryExecutedLabel">Query Executed: </div>
            <textarea id="queryExecuted" style="width:600px;height:100px" readonly="readonly"></textarea>
            <div id="queryResultsTable"></div>
        </div>
    </body>
</html>
